{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ac2c65db-ea0c-4703-ba4e-d1b78019e41a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fa2ecb22-0762-4d0a-8577-08e3eed91bad",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data = [10,11,12]\n",
    "index = [\"a\", \"b\", \"c\"]\n",
    "s = pd.Series(data = data, index = index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1a76014f-91f7-4a84-ac37-d1254f588140",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    10\n",
       "b    11\n",
       "c    12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "011f192e-a59a-409f-9cb2-d7ac9bc5fd72",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9ae9400e-bc6e-4aed-aea9-a7e5f7a3be07",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    10\n",
       "b    11\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f995a82d-b32b-400e-acf8-977ddb97c663",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    10\n",
       "c    12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = [True, False, True]\n",
    "s[mask]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f127e562-f82e-4d9d-aa81-04e63c1787ec",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.loc[\"b\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6ca05eaf-4087-48b3-a57f-21dede6ec6c3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.iloc[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6e344f8b-f0ed-46d4-9b46-40bd8eb20561",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    100\n",
       "b     11\n",
       "c     12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = s.copy()\n",
    "s1[\"a\"] = 100\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "43813c75-0c3e-4a1f-9365-f87d5333cf86",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    101\n",
       "b     11\n",
       "c     12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.replace(to_replace = 100, value = 101, inplace = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b890afbf-8cc5-4b4d-b5a4-6a0e6ceff546",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    100\n",
       "b     11\n",
       "c     12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68c47a07-5a45-4f83-98c8-6332528f66f3",
   "metadata": {
    "tags": []
   },
   "source": [
    "inplace = True时，数值才会真的被改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3964743d-2076-4951-b86b-8f94a2f05de6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "s1.replace(to_replace = 100, value = 101, inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "665dad26-8ea7-4444-9215-38060a0605ef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    101\n",
       "b     11\n",
       "c     12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "64833248-3ff1-45d4-8940-bc1310c49966",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    101\n",
       "b     11\n",
       "d     12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.index  = [\"a\", \"b\", \"d\"]\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "45eb2d05-a84c-4141-a6a2-b902e86c3eec",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    101\n",
       "b     11\n",
       "d     12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.rename(index={\"a\":\"A\"}, inplace = True)\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ee7b3dda-3ec2-43fc-983f-954efbbd2cad",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "s2 = pd.Series([100, 500], index=[\"g\", \"h\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64640b59-609f-4e33-b6a6-9d06b35d7c0c",
   "metadata": {},
   "source": [
    "增操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f1303c87-91c9-4019-b2fb-daa1605b28b4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    101\n",
       "b     11\n",
       "d     12\n",
       "g    100\n",
       "h    500\n",
       "dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = pd.concat([s1, s2])\n",
    "s1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "669d15ca-c68d-4629-9753-4ee4fbaa8341",
   "metadata": {},
   "source": [
    "删操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "47ab059d-d457-4681-b92e-9efb24dd9a79",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    101\n",
       "b     11\n",
       "d     12\n",
       "g    100\n",
       "h    500\n",
       "dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3b869ab3-0639-4b2c-9277-e0355e8e1e42",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    101\n",
       "d     12\n",
       "g    100\n",
       "h    500\n",
       "dtype: int64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del(s1[\"b\"])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "43e71bcf-0c29-4c9b-b4dd-e54ca535e75e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    101\n",
       "d     12\n",
       "g    100\n",
       "h    500\n",
       "dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.drop([\"g\", \"d\"])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a65f725e-7e1d-48cc-b8fa-13edf54a1f05",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    101\n",
       "h    500\n",
       "dtype: int64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.drop([\"g\", \"d\"], inplace=True)\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "84fe6bcb-fcb9-40cf-89d7-b7d9621ce762",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B  C\n",
       "a  1  2  3\n",
       "b  4  5  6"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(data=[[1,2,3], [4,5,6]],\n",
    "                 index=[\"a\", \"b\"],\n",
    "                 columns=[\"A\", \"B\", \"C\"])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f45e5fc7-1645-49fb-8adf-d84149ac8794",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    4\n",
       "Name: A, dtype: int64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"A\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "671cd2b2-21d9-41b6-9cd6-066cc40089fa",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1\n",
       "B    2\n",
       "C    3\n",
       "Name: a, dtype: int64"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[\"a\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "9743f46a-ad75-408f-916a-57af563b6cad",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>fang</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A  B  C  fang\n",
       "a  1  2  3    10\n",
       "b  4  5  6    10"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"fang\"] = [10,10]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4a9f675-7859-4520-951f-8ee27c61cb2c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
